package br.ucb.ucblive.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.sun.org.apache.bcel.internal.generic.RETURN;

import br.ucb.ucblive.HIB.CursoHIB;
import br.ucb.ucblive.beans.Curso;


@FacesConverter (value="CursoConverter")
public class CursoConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		CursoHIB cursoHib = new CursoHIB();
		if (value == null)
			return null;
		else if(value.toString().equals(""))
			return null;
		try {
			return cursoHib.consultar(Integer.parseInt(value));
		} catch (Exception e) {
			e.printStackTrace();
			return new Object();
		}
	}

	@Override
	public String getAsString(FacesContext context, UIComponent componente, Object object) {
		if (object == null)
			return null;
		if(object.toString().equals(""))
			return null;
		Curso curso = (Curso) object;		
		return Integer.toString(curso.getIdCurso());
	}

}
